package com.one.rope.mvp.web.controller.admin;

import com.one.rope.mvp.basecore.annotation.LogMethod;
import com.one.rope.mvp.web.business.service.ICompanyMemberService;
import com.one.rope.mvp.web.business.service.ICompanyProjectPartnerService;
import com.one.rope.mvp.web.business.service.ICompanyProjectService;
import com.one.rope.mvp.web.business.service.ICompanyService;
import com.one.rope.mvp.web.common.dto.admin.company.AdminCompanyDetailVO;
import com.one.rope.mvp.web.common.dto.admin.company.AdminCompanyListDTO;
import com.one.rope.mvp.web.common.dto.admin.company.AdminCompanyListItemVO;
import com.one.rope.mvp.web.common.dto.admin.company.EditCompanyDTO;
import com.one.rope.mvp.web.common.dto.admin.partner.AdminEditPartnerDTO;
import com.one.rope.mvp.web.common.dto.admin.project.AdminEditProjectDTO;
import com.one.rope.mvp.web.common.dto.admin.project.AdminProjectDetailVO;
import com.one.rope.mvp.web.common.dto.admin.team.SaveTeamMemberDTO;
import com.one.rope.mvp.web.common.dto.admin.team.TeamMemberDetailVO;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.web.common.util.ObjectUtils;
import com.one.rope.mvp.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Weijian.Liu
 * @desc 管理后台企业数据控制器
 * @date 2020/2/20 10:53
 */
@Api(tags = "A2管理后台-主体数据模块")
@RestController
@RequestMapping("/admin/api")
@Slf4j
public class AdminCompanyController extends BaseController {

  @Autowired
  ICompanyService companyService;

  @Autowired
  ICompanyProjectService companyProjectService;

  @Autowired
  ICompanyProjectPartnerService companyProjectPartnerService;

  @Autowired
  ICompanyMemberService companyMemberService;

  @LogMethod
  @ApiOperation(value = "001企业列表", notes = "企业列表接口", response = AdminCompanyListItemVO.class)
  @PostMapping(value = "/company/companyList")
  public ResponseObject companyList(@RequestBody AdminCompanyListDTO requestDTO) {
    try {
      return companyService.companyList(requestDTO);
    } catch (Exception e) {
      log.error("/admin/api/company/companyList接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "002企业详情", notes = "企业详情接口", response = AdminCompanyDetailVO.class)
  @GetMapping(value = "/company/companyDetail/{id}")
  public ResponseObject companyDetail(@PathVariable("id") Long id) {
    try {
      return companyService.companyDetail(id);
    } catch (Exception e) {
      log.error("/admin/api/company/companyDetail接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "003企业编辑", notes = "企业编辑接口")
  @PostMapping(value = "/company/editCompany")
  public ResponseObject editCompany(@RequestBody EditCompanyDTO requestDTO) {
    ObjectUtils.requestNonNull(requestDTO.getId(), "企业id不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getName(), "名称不能为空");
    try {
      companyService.editCompany(requestDTO);
      return ResponseObject.successObject();
    } catch (Exception e) {
      log.error("/admin/api/company/editCompany接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "004企业项目列表-下拉框", notes = "企业项目列表接口", response = AdminProjectDetailVO.class)
  @GetMapping(value = "/company/projectList/{companyId}")
  public ResponseObject projectList(@PathVariable("companyId") Long companyId) {
    try {
      return companyProjectService.companyProjectList(companyId);
    } catch (Exception e) {
      log.error("/admin/api/company/projectList接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "005项目详情", notes = "项目详情接口", response = AdminProjectDetailVO.class)
  @GetMapping(value = "/company/projectDetail/{id}")
  public ResponseObject projectDetail(@PathVariable("id") Long id) {
    try {
      return companyProjectService.projectDetail(id);
    } catch (Exception e) {
      log.error("/admin/api/company/projectDetail接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "006项目编辑", notes = "项目编辑接口")
  @PostMapping(value = "/company/editProject")
  public ResponseObject editProject(@RequestBody AdminEditProjectDTO requestDTO) {
    ObjectUtils.requestNonNull(requestDTO.getId(), "项目id不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getName(), "名称不能为空");
    try {
      return companyProjectService.editProject(requestDTO);
    } catch (Exception e) {
      log.error("/admin/api/company/editProject接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "007项目删除", notes = "项目删除接口")
  @GetMapping(value = "/company/deleteProject/{id}")
  public ResponseObject deleteProject(@PathVariable("id") Long id) {
    try {
      return companyProjectService.deleteProject(id);
    } catch (Exception e) {
      log.error("/admin/api/company/deleteProject接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "008合伙人详情", notes = "合伙人详情接口", response = AdminProjectDetailVO.class)
  @GetMapping(value = "/company/partnerDetail/{id}")
  public ResponseObject partnerDetail(@PathVariable("id") Long id) {
    try {
      return new ResponseObject(companyProjectPartnerService.partnerDetail(id));
    } catch (Exception e) {
      log.error("/admin/api/company/partnerDetail接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "009合伙人编辑", notes = "合伙人编辑接口")
  @PostMapping(value = "/company/editPartner")
  public ResponseObject editPartner(@RequestBody AdminEditPartnerDTO requestDTO) {
    ObjectUtils.requestNonNull(requestDTO.getId(), "合伙人id不能为空");
    ObjectUtils.requestNonNull(requestDTO.getPartnerType(), "合伙人类型不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getName(), "岗位名称不能为空");
    try {
      return companyProjectPartnerService.editPartner(requestDTO);
    } catch (Exception e) {
      log.error("/admin/api/company/editPartner接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "010合伙人删除", notes = "合伙人删除接口")
  @GetMapping(value = "/company/deletePartner/{id}")
  public ResponseObject deletePartner(@PathVariable("id") Long id) {
    try {
      companyProjectPartnerService.deletePartner(id);
      return ResponseObject.successObject();
    } catch (Exception e) {
      log.error("/admin/api/company/deletePartner接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "011团队成员详情", notes = "团队成员详情接口", response = TeamMemberDetailVO.class)
  @GetMapping(value = "/company/teamMemberDetail/{id}")
  public ResponseObject teamMemberDetail(@PathVariable("id") Long id) {
    try {
      return companyMemberService.teamMemberDetail(id);
    } catch (Exception e) {
      log.error("/admin/api/company/teamMemberDetail接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "012团队成员编辑", notes = "团队成员编辑接口")
  @PostMapping(value = "/company/editTeamMember")
  public ResponseObject editTeamMember(@RequestBody SaveTeamMemberDTO requestDTO) {
    ObjectUtils.requestNonNull(requestDTO.getId(), "成员id不能为空");
    ObjectUtils.requestNonNull(requestDTO.getSex(), "称谓不能为空");
    ObjectUtils.requestNonEmpty(requestDTO.getName(), "姓名不能为空");
    try {
      return companyMemberService.saveTeamMember(requestDTO);
    } catch (Exception e) {
      log.error("/admin/api/company/editTeamMember接口异常", e);
      return ResponseObject.systemError(e);
    }
  }

  @LogMethod
  @ApiOperation(value = "013团队成员删除", notes = "团队成员删除接口")
  @GetMapping(value = "/company/deleteTeamMember/{id}")
  public ResponseObject deleteTeamMember(@PathVariable("id") Long id) {
    try {
      return companyMemberService.deleteTeamMember(id);
    } catch (Exception e) {
      log.error("/admin/api/company/deleteTeamMember接口异常", e);
      return ResponseObject.systemError(e);
    }
  }
}
